﻿using System;
using System.Text;
using Rhino.ServiceBus.Hosting;
using Utils;
using Messages;
using Rhino.ServiceBus;
using System.Collections.Generic;

namespace PubServer
{
    class Program
    {
        static void Main(string[] args)
        {
            Queue.PrepareQueue("server");

            List<string> listaPrzedmiotow = new List<string>();

            listaPrzedmiotow.Add("Coffee");
            listaPrzedmiotow.Add("Tea");
            listaPrzedmiotow.Add("Ice cream");


            Console.WriteLine("Server is waiting for messages....");

            var i = 0;
            while ( i < listaPrzedmiotow.Count - 1 ) 
            {
                Console.WriteLine("Avaible items: " + listaPrzedmiotow[i]);
                i++;
            }

            var host = new DefaultHost();
            host.Start<BootStrapper>();

            Console.ReadLine();
        }
    }

    public class MessageInterceptAndResponse : ConsumerOf<Message_Type1>
    {
        private readonly IServiceBus _serviceBus;
        private List<string> listaPrzedmiotow = new List<string>();

        public MessageInterceptAndResponse(IServiceBus serviceBus)
        {
            _serviceBus = serviceBus;
            listaPrzedmiotow.Add("Coffee");
            listaPrzedmiotow.Add("Tea");
            listaPrzedmiotow.Add("Ice cream");
        }

        public void Consume(Message_Type1 message)
        {
            Console.WriteLine("Sub" + message.Id.ToString() + " want to buy: " + message.Text);

            if (listaPrzedmiotow.Contains(message.Text))
            {
                _serviceBus.Publish(new Message_Type2
                {
                    Text = "Sub" + message.Id.ToString() + " has bought " + message.Text
                });
            }
            else
            {
                _serviceBus.Publish(new Message_Type2
                {
                    Text = "This item is not available: " + message.Text
                });
            }
        }
    }
}
